<template>
  <div v-title data-title="料箱呼叫">
    <el-row :gutter="12">
      <el-card>
        <el-form
          ref="searchForm"
          :inline="true"
          :model="searchForm"
          label-width="9vh"
          class="search_form"
        >
          <el-form-item label="托盘" prop="CID">
            <el-input
              v-model="searchForm.CID"
              placeholder="托盘编码"
              clearable
            />
          </el-form-item>
          <el-form-item label="库位" prop="LocationId">
            <el-input
              v-model="searchForm.LocationId"
              placeholder="库位编码"
              clearable
            />
          </el-form-item>
          <el-form-item label="库区" prop="WHAreaId">
            <el-select v-model="searchForm.WHAreaId" placeholder="库区">
              <el-option
                v-for="item in WHAreaIdS"
                :key="item.WHAreaId"
                :label="item.WHAreaName"
                :value="item.WHAreaId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="排序字段" prop="Sort">
						<el-select v-model="searchForm.Sort" placeholder="排序字段">
							<el-option v-for="item in sortList" :key="item.SortField" :label="item.SortName"
								:value="item.SortField">
							</el-option>
						</el-select>
					</el-form-item>
					 <el-form-item label="排序方式" prop="SortMode">
						<el-select v-model="searchForm.SortMode" placeholder="排序方式">
							<el-option v-for="item in sortmode" :key="item.sortmodeField" :label="item.sortmodename"
								:value="item.sortmodeField">
							</el-option>
						</el-select>
					</el-form-item> -->
          <el-form-item>
            <el-button type="primary" @click="SubClick">查询</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card>
        <el-table
          :data="tableData"
          border
          :row-style="{ height: '50px', 'font-size': '20px' }"
          style="width: 100%"
          :cell-style="{ padding: '0px' }"
          :header-row-style="{ height: '50px' }"
          :height="tableHeight"
          ref="tableRef"
          :header-cell-style="{
            height: '50px',
            padding: '0px',
            'font-size': '20px',
            background: '#18bb9c',
            color: '#fff',
            'text-align': 'center',
          }"
        >
          <el-table-column fix type="selection" width="40" :align="align">
          </el-table-column>
          <el-table-column
            prop="WHAreaId"
            :width="tableHeight * 0.17"
            label="库区"
            :align="align"
          >
          </el-table-column>
          <el-table-column
            prop="LocationId"
            label="库位"
            :width="tableHeight * 0.4"
            :align="align"
          >
          </el-table-column>
          <el-table-column
            prop="CID"
            label="托盘编码"
            :width="tableHeight * 0.3"
            :align="align"
          >
          </el-table-column>
          <el-table-column
            prop="Qty"
            label="库存数"
            :width="tableHeight * 0.2"
            :align="align"
          >
          </el-table-column>
          <el-table-column
            prop="CreateBy"
            label="创建人"
            :width="tableHeight * 0.2"
            :align="align"
          >
          </el-table-column>
          <el-table-column
            prop="CreateDate"
            :width="tableHeight * 0.4"
            label="创建时间"
            :align="align"
          >
          </el-table-column>
          <el-table-column
            prop="ModifyBy"
            label="修改人"
            :width="tableHeight * 0.2"
            :align="align"
          >
          </el-table-column>
          <el-table-column
            prop="ModifyDate"
            :width="tableHeight * 0.4"
            label="修改时间"
            :align="align"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            :width="tableHeight * 0.25"
            :align="align"
          >
            <template slot-scope="scope">
              <el-button
                @click="CallBoxClick(scope.row)"
                type="text"
                :disabled="
                  CallBox.indexOf(scope.row.WHAreaType) == -1 ? true : false
                "
                style="font-size: 1.5vh"
                >呼叫托盘</el-button
              >
              <el-button
                type="text"
                @click="returnBoxClick(scope.row)"
                :disabled="
                  (scope.row.isTDS != 1 || parseFloat(scope.row.Qty) != 0.0) &&
                  returnBox.indexOf(scope.row.WHAreaType) == -1
                    ? true
                    : false
                "
                style="font-size: 1.5vh; margin-left: 1vh"
                >返回托盘</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[20, 40, 100, 200]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            style="margin-top: 1vh; display: flex"
          >
          </el-pagination>
        </div>
      </el-card>
    </el-row>
  </div>
</template>

<script>
import {
  GETMaterialBox,
  POSTMaterialBox,
  GETWHAreaInfo,
  PUTMaterialBox
} from '@/api/api.js'
export default {
  data () {
    return {
      align: 'center',
      returnBox: ['TMP_STORAGE', 'EXCHANGE', 'RETURN', 'PRODUCT'],
      CallBox: ['STORAGE', 'PICK', 'EMPTY_PALLET_GROUP'],
      tableHeight: 300,
      sortmode: [{
        sortmodename: "升序",
        sortmodeField: "ASC"
      },
      {
        sortmodename: "降序",
        sortmodeField: "Desc"
      }
      ],
      sortList: [
        {
          SortName: '托盘编码',
          SortField: 'CID'
        },
        {
          SortName: '库位编码',
          SortField: 'LocationId'
        },
        {
          SortName: '库存数',
          SortField: 'Qty'
        }
      ],
      searchForm: {
        Sort: '',
        SortMode: 'ASC',
        LocationId: '',
        SkuId: '',
        CID: '',
        WHAreaId: ''
      },
      WHAreaIdS: [],
      currentPage: 1,
      total: 0,
      pageSize: 20,
      tableData: []
    }
  },
  created () {
    window.removeEventListener('onmessageWS', this.getSocketData);
    this.UserInfo = JSON.parse(sessionStorage.getItem('userInfo'))
    this.WHAreainfo()
    this.onSubmit()
  },
  methods: {
    SubClick () {
      this.currentPage = 1
      this.onSubmit()
    },
    onSubmit () {
      const params = this.searchForm
      params.pageIndex = this.currentPage - 1
      params.pageSize = this.pageSize
      GETMaterialBox(params).then(res => {
        const result = res.data.data
        if (res.data.code == 0) {
          this.tableData = result
          this.total = res.data.totalCount
          console.log(result)
        } else {
          this.$message({
            message: res.data.message,
            type: 'error'
          });
        }
      })
    },
    CallBoxClick (row) {
      const params = row
      params.WbId = this.UserInfo.WbId
      params.UserId = this.UserInfo.UserId
      params.time = Date.parse(new Date())
      this.$confirm('将托盘叫到当前工作台, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        POSTMaterialBox(params).then(res => {
          if (res.data.code === 0) {
            this.$message({
              type: 'success',
              message: '任务生成成功!'
            });
          } else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    returnBoxClick (row) {
      const params = row
      params.WbId = this.UserInfo.WbId
      params.UserId = this.UserInfo.UserId
      params.time = Date.parse(new Date())
      this.$confirm('托盘返回原库区, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        PUTMaterialBox(params).then(res => {
          if (res.data.code == 0) {
            this.$message({
              type: 'success',
              message: '任务生成成功!'
            });
          } else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSizeChange (val) {
      this.currentPage = 1
      this.pageSize = val
      this.onSubmit()
    },
    handleCurrentChange (val) {
      this.currentPage = val
      this.onSubmit()
    },
    selectClear () {
      this.$refs.tableRef.clearSelection();
    },
    resetForm () {
      this.$refs.searchForm.resetFields()
    },
    WHAreainfo () {
      const params = {}
      GETWHAreaInfo(params).then(res => {
        const result = res.data.data
        if (res.data.code == 0) {
          this.WHAreaIdS = result
        } else {
          this.$message({
            message: res.data.message,
            type: 'error'
          });
        }
      })
    }
  },
  watch: {
    searchVal: function (val) {
      console.log(val); // 接收父组件的值
      this.WindowsSize = val;
    }
  },
  mounted () {
    let that = this
    this.$nextTick(() => {
      //窗口高度 - 表格距离顶部距离 - 需求高度
      this.tableHeight = window.innerHeight - this.$refs.tableRef.$el.offsetTop - 100
      this.$refs.tableRef.doLayout() // 更新表格 防止表格内容变化表格异常情况
      // 监听窗口大小变化
      window.onresize = function () {
        that.tableHeight =
          window.innerHeight - that.$refs.tableRef.$el.offsetTop - 90
        that.$refs.tableRef.doLayout()
      }
    })
  },
}
</script>

<style lang="scss">
.search-form {
  // padding-top: 18px;
  // margin-bottom: 15px;
  background-color: #f7f8fb;

  .el-form-item__label {
    font-size: 11px !important;
  }

  display: flex;
}
</style>
